package cn.com.elite;


import java.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;

import com.elite.algorithm.GUID;
import com.elitecrm.saas.service.EliteSAASException;

public class LoadMeInfoService {
	private static final Log log = LogFactory.getLog(LoadMeInfoService.class);
	
	public String loadMeInfoService(DataSource dataSource,String ep_id,Document doc,String HandleGroup_ID,String Staff_ID)throws Exception{
		String ReturnXml="";
		Element ExamMainIDEle=(Element)doc.selectSingleNode("/Root/ExamMainID");
		if(ExamMainIDEle!=null){
			String ExamMainID=ExamMainIDEle.getText();
			if(ExamMainID!=null){
				Element SchemeIDEle=(Element)doc.selectSingleNode("/Root/SchemeID");
				if(SchemeIDEle!=null){
					String SchemeID=SchemeIDEle.getText();
					if(SchemeID!=null){
						Element ExamResultMainIDEle=(Element)doc.selectSingleNode("/Root/ExamResultMainID");
						if(ExamResultMainIDEle!=null){
							String ExamResultMainID=ExamResultMainIDEle.getText();
							ReturnXml=loadMeInfoServiceImpl(dataSource,ep_id,ExamMainID,ExamResultMainID,HandleGroup_ID,Staff_ID,SchemeID);
						}else{
							throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"ExamResultMainIDEle==null");
						}
					}else{
						throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"SchemeIDEle.getText()==null");
					}
				}else{
					throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"SchemeIDEle==null");
				}
				
			}else{
				throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"ExamMainIDEle.getText()==null");
			}
		}else{
			throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"ExamMainIDEle==null");
		}
		return ReturnXml;
	}
	public int loadExamResultMain(DataSource dataSource,String ExamResultMainID,String ep_id,String HandleGroup_ID,String Staff_ID,String SchemeID,String ExamMainID,int FirstIn,int LastIn)throws Exception{
		int result=0;
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String BeginTime=df.format(new Date());
		String W_ExamResultMain="W_ExamResultMain_"+ep_id;
		String sql=" insert into "+W_ExamResultMain+"(ExamResultMainID,HandleGroup_ID,Staff_ID,SchemeID,ExamMainID,FirstIn,LastIn,BeginTime)Values(?,?,?,?,?,?,?,?)";
		Object[] params=new Object[]{ExamResultMainID,HandleGroup_ID,Staff_ID,SchemeID,ExamMainID,FirstIn,LastIn,BeginTime};  
		int[] types=new int[]{Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.INTEGER,Types.INTEGER,Types.TIMESTAMP};  
		result=util.UpdatePersonUseExecute(sql,types,params,dataSource);
		return result;
	}
	public int KSHis(DataSource dataSource,String ep_id,String Staff_ID,String SchemeID,String ExamMainID) throws Exception{
		int result=0;
		String W_ExamResultMain="W_ExamResultMain_"+ep_id;
		String sql="select * from "+W_ExamResultMain+" where Staff_ID=? and SchemeID=? and ExamMainID=?";
		int[] types=new int[]{Types.VARCHAR,Types.VARCHAR,Types.VARCHAR};
		Object[] params=new Object[]{Staff_ID,SchemeID,ExamMainID};
        List list=util.QueryPersonUseExecute(sql,types,params,dataSource);
        if(list!=null&&list.size()>0){
        	result=list.size();
        }
		return result;
	}
	public static String getTimeStampNumberFormat(Timestamp formatTime){
		SimpleDateFormat m_format=new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss",new Locale("zh","cn"));
        return m_format.format(formatTime);
	}
	public String loadMeInfoServiceImpl(DataSource dataSource,String ep_id,String ExamMainID,String ExamResultMainID,String HandleGroup_ID,String Staff_ID,String SchemeID)throws Exception{
		String ReturnXml="";
		Document result=util.getDoc();
		Element root=result.addElement("R");
		root.addElement("Result").addText("1");
		int kshis=KSHis(dataSource,ep_id,Staff_ID,SchemeID,ExamMainID);
		root.addElement("KSHis").addText(kshis+"");
		
		String W_ExamScheme="W_ExamScheme_"+ep_id;
		String sql3="select ExamTime from "+W_ExamScheme+" where SchemeID=?";
		Object[] params3=new Object[]{SchemeID};  
		int[] types3=new int[]{Types.VARCHAR};  
		List list3=util.QueryPersonUseExecute(sql3,types3,params3,dataSource);
		Map map3=(Map)list3.get(0);
		int ExamTime=(Integer)map3.get("ExamTime");
		
		if(ExamResultMainID!=null&&!ExamResultMainID.equals("")&&ExamResultMainID.length()>0){
			root.addElement("SchemeID").addText(SchemeID);
			root.addElement("ExamMainID").addText(ExamMainID);
			root.addElement("ExamResultMainID").addText(ExamResultMainID);
			if(ExamTime==0){
				root.addElement("RemainTime").addText("");
			}else{
				String W_ExamResultMain="W_ExamResultMain_"+ep_id;
				String sql="select BeginTime from "+W_ExamResultMain+" where ExamResultMainID=?";
				Object[] params=new Object[]{ExamResultMainID};  
				int[] types=new int[]{Types.VARCHAR};  
				List list=util.QueryPersonUseExecute(sql,types,params,dataSource);
				Map map4=(Map)list.get(0);
				Timestamp BeginTime=(Timestamp)map4.get("BeginTime");
				SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss");
		        long t1=0L;
		        long t2=0L;
		        t1=timeformat.parse(getTimeStampNumberFormat(BeginTime)).getTime();
		        t2=timeformat.parse(getTimeStampNumberFormat(new Timestamp(new java.util.Date().getTime()))).getTime();
		        long deff=(t1-t2)/1000;
		        deff=ExamTime+deff;
		        root.addElement("RemainTime").addText(deff+"");
			}
		}else{
			GUID guid=new GUID();
			ExamResultMainID=guid.toString();
			log.debug("ExamResultMainID:"+ExamResultMainID);
			int loadExamResultMain_result=loadExamResultMain(dataSource,ExamResultMainID,ep_id,HandleGroup_ID,Staff_ID,SchemeID,ExamMainID,0,1);
			if(loadExamResultMain_result==1){
				root.addElement("SchemeID").addText(SchemeID);
				root.addElement("ExamMainID").addText(ExamMainID);
				root.addElement("ExamResultMainID").addText(ExamResultMainID);
				if(ExamTime==0){
					root.addElement("RemainTime").addText("");
				}else{
					root.addElement("RemainTime").addText(ExamTime+"");
				}
			}else{
				throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"loadMeInfoServiceImpl_fail");
			}
		}
		String W_ExamList="W_ExamList_"+ep_id;
		String W_QuestionList="W_QuestionList_"+ep_id;
		String W_QuestionType="W_QuestionType";
		String sql="select "+W_ExamList+".ExamListID,"+W_QuestionList+".QListID,QResult,QTypeKey,QDesc,VoicePath,PicturePath,Score " +
				"from "+W_ExamList+" " +
				"left join "+W_QuestionList+" on "+W_ExamList+".QListID="+W_QuestionList+".QListID " +
				"left join "+W_QuestionType+" on "+W_QuestionList+".QType="+W_QuestionType+".QTypeID " +
				"where "+W_ExamList+".ExamMainID=? order by "+W_ExamList+".ListIndex asc";
		log.debug("loadMeInfoServiceImpl_sql:"+sql);
		Object[] params=new Object[]{ExamMainID};  
		int[] types=new int[]{Types.VARCHAR};  
		List KSList=util.QueryPersonUseExecute(sql,types,params,dataSource);
		if(KSList!=null&&KSList.size()>0){
			root.addElement("KsresultLEN").addText(KSList.size()+"");
	    	for(int i=0;i<KSList.size();i++){
	    		Map map=(Map)KSList.get(i);
	    		Element kslist=root.addElement("Ksresult");
	    		
	    		String ExamListID=(String)map.get("ExamListID");
	    		log.debug("ExamListID:"+ExamListID);
	    		kslist.addElement("ExamListID").addText(ExamListID);
	    		
	    		String QListID=(String)map.get("QListID");
	    		log.debug("QListID:"+QListID);
	    		kslist.addElement("QListID").addText(QListID);
	    		
	    		String QTypeKey=(String)map.get("QTypeKey");
	    		log.debug("QTypeKey:"+QTypeKey);
	    		kslist.addElement("QTypeKey").addText(QTypeKey);
	    		
	    		String QDesc=(String)map.get("QDesc");
	    		log.debug("QDesc:"+QDesc);
	    		kslist.addElement("QDesc").addText(QDesc);
	    		
	    		String QResult=(String)map.get("QResult");
	    		log.debug("QResult:"+QResult);
	    		kslist.addElement("QResult").addText(QResult);
	    		
	    		String VoicePath=(String)map.get("VoicePath");
	    		if(VoicePath==null){VoicePath="";}
	    		log.debug("VoicePath:"+VoicePath);
	    		kslist.addElement("VoicePath").addText(VoicePath);
	    		
	    		String Picturepath=(String)map.get("PicturePath");
	    		if(Picturepath==null){Picturepath="";}
	    		log.debug("Picturepath:"+Picturepath);
	    		kslist.addElement("Picturepath").addText(Picturepath);
	    	
	    		String Score=Double.toString((Double)map.get("Score"));
	    		log.debug("Score:"+Score);
	    		kslist.addElement("Score").addText(Score);
	    		
	    		String W_QuestionChoice="W_QuestionChoice_"+ep_id;
	    		String sql1="select choiceindex,choicekey,choiceinfo,picturepath from "+W_QuestionChoice+" where QListID=? order by choiceindex asc";
	    		Object[] params1=new Object[]{QListID};  
	    		int[] types1=new int[]{Types.VARCHAR}; 
	    		List KSList1=util.QueryPersonUseExecute(sql1,types1,params1,dataSource);
	    		if(KSList1!=null&&KSList1.size()>0){
	    			for(int m=0;m<KSList1.size();m++){
	    				Element QDesc_list=kslist.addElement("QDesc_list");
	    				Map map1=(Map)KSList1.get(m);
	    				
	    				String choiceindex=Integer.toString((Integer)map1.get("choiceindex"));
	    	    		log.debug("choiceindex:"+choiceindex);
	    	    		QDesc_list.addElement("choiceindex").addText(choiceindex);
	    	    		
	    	    		String choicekey=(String)map1.get("choicekey");
	    	    		log.debug("choicekey:"+choicekey);
	    	    		QDesc_list.addElement("choicekey").addText(choicekey);
	    	    		String choiceinfo=(String)map1.get("choiceinfo");
	    	    		if(choiceinfo==null){choiceinfo="";}
	    	    		log.debug("choiceinfo:"+choiceinfo);
	    	    		QDesc_list.addElement("choiceinfo").addText(choiceinfo);
	    	    		String picturepath=(String)map1.get("picturepath");
	    	    		if(picturepath==null){picturepath="";}
	    	    		log.debug("picturepath:"+picturepath);
	    	    		QDesc_list.addElement("picturepath").addText(picturepath);
	    			}	
	    		}
	    	}
		}else{
			root.addElement("KsresultLEN").addText("0");
		}
		ReturnXml=result.asXML();
		log.debug("loadMeInfoServiceImpl_ReturnXml"+ReturnXml);
		return ReturnXml;
		
	}
}
